//
//  YSMeZhangDanViewController.m
//  YouSheng
//
//  Created by Mac on 16/9/1.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "YSMeZhangDanViewController.h"
#import "UIView+Frame.h"

#define YSBJColorLan [UIColor colorWithRed:78/255.0 green:204/255.0 blue:190/255.0 alpha:1]

@interface YSMeZhangDanViewController ()

@property (nonatomic, strong) NSMutableArray *titleButtons;

@property (nonatomic, weak) UIView *underLineView;

@property (nonatomic, weak) UIButton *selButton;
@property (nonatomic, assign) BOOL isInitial;
@property (nonatomic, weak) UIScrollView *topView;

@end

@implementation YSMeZhangDanViewController

- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self setupTopView];
//    [self setupAllTitle];
    
    // iOS7之后，如果是导航控制器下scrollView,顶部都会添加64的额外滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // dispatch_once:整个app运行过程中 只会执行一次
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    if (_isInitial == NO) {
        
        // 设置所有标题
        [self setupAllTitle];
        
        _isInitial = YES;
    }
    //    });
    
}

// 设置所有标题
- (void)setupAllTitle
{
//    NSInteger count = self.childViewControllers.count;
    CGFloat btnW = UIScreenWidth / 2;
    CGFloat btnH = _topView.xmg_height;
    CGFloat btnX = 0;
    for (int i = 0; i < 2; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        btnX = i * btnW;
        titleButton.tag = i;
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
//        [titleButton setTitleColor:YSBJColorLan forState:UIControlStateSelected];
    
        NSString *title = @"通 话";
        if (i == 1) {
            title = @"礼 物";
        }
        [titleButton setTitle:title forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:YSBJColorLan forState:UIControlStateSelected];
        // 设置标题字体
        titleButton.titleLabel.font = [UIFont systemFontOfSize:18];
        // 监听标题按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:titleButton];
        [self.titleButtons addObject:titleButton];
        
        // 默认选中第0个按钮
        if (i == 0) {
            [self titleClick:titleButton];
            // 下划线宽度 = 按钮文字宽度
            // 下划线中心点的x = 按钮中心点x
            // 下划线的高度 = 2
            // 下划线的y = topView的height - 2
            // 添加下划线
            // 按钮的标题的宽度不会马上计算，只能手动计算文字宽度
            UIView *underLineView = [[UIView alloc] init];
            underLineView.backgroundColor = YSBJColorLan;
            // 注意：设置中心点，一点要先设置尺寸
            underLineView.xmg_width = [title sizeWithFont:[UIFont systemFontOfSize:18]].width * 2;
            underLineView.xmg_centerX = titleButton.xmg_centerX;
            
            underLineView.xmg_height = 2;
            underLineView.xmg_y = _topView.xmg_height - underLineView.xmg_height;
            [_topView addSubview:underLineView];
            _underLineView = underLineView;
        }
    }
}

- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.选中按钮
    [self selButton:button];
    
    // 2.让collectionView滚动到对应位置,并不是马上滚动
    CGFloat offsetX = i * UIScreenWidth;
//    _collectionView.contentOffset = CGPointMake(offsetX, 0);
    
    
    /*
     问题：点击标题，拿不到cell，返回cell方法根本就没有调用
     分析：返回cell方法为什么没有调用 因为没有滚动
     即使滚动了 也拿不到，contentOffset 延迟 ，并不是马上滚动。
     
     */
}

#pragma mark - 选中按钮
- (void)selButton:(UIButton *)button
{
    _selButton.selected = NO;
    button.selected = YES;
    _selButton = button;
    
    // 移动下划线
    [UIView animateWithDuration:0.25 animations:^{
        
        _underLineView.xmg_centerX = button.xmg_centerX;
        _underLineView.xmg_width = button.titleLabel.xmg_width * 2;
    }];
}

// 搭建顶部view: UIScollowView
- (void)setupTopView
{
    UIScrollView *topView = [[UIScrollView alloc] init];
    topView.scrollsToTop = NO;
    _topView = topView;
    topView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    CGFloat topY = self.navigationController.navigationBarHidden? 20 : 64;
    CGFloat topH = 44;
    CGFloat topW = UIScreenWidth;
    topView.frame = CGRectMake(0, topY, topW, topH);
    [self.view addSubview:topView];
}




@end
